﻿using System;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfCommGoodsPublic : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                this.SetContrlValue("txtId",GetFormParam("ID"));
                ShowData(GetFormParam("ID"));
            }
        }
        private void BindControls()
        {
            if (!IsPostBack)
            {
                BoundLevelOne();
                BoundCtLevelOne();
                using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oCBizShpGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
                {
                    DataSet ds = oCBizShpGoodsInfo.SelectGoodBrandForPublish(this.oUserInfo.OperDealerId.Split(',')[0]);
                    this.BoundComboBox("dplBRAND_ID", ds, "BRAND_NME", "BRAND_ID", "", "", true);
                }
                using (msBsc.AppLogic.DataAlc.CBizShpCommercialLogistics oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialLogistics())
                {

                    DataSet ds = oCDbAccess.SelectTempNmeByDealerId(this.oUserInfo.OperDealerId.Split(',')[0]);
                    this.BoundComboBox("dplLOGISTICS_TEMPLATE_ID", ds, "TEMPLATE_NME", "LOGISTICS_TEMPLATE_ID", "", "", false);
                }
                using (msBsc.AppLogic.DataAlc.CBizShpCommercialShipping oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialShipping())
                {

                    DataSet ds = oCDbAccess.SelectBoundByDealerId(this.oUserInfo.OperDealerId.Split(',')[0]);
                    this.BoundComboBox("dplSHIP_ADDRESS_ID", ds, "ZONE_DESC", "SHIP_ADDRESS_ID", "", "", false);
                }
                BoundFromProvince();
            }
        }



        private void BoundFromProvince()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplFROM_PROVINCE_ID", ds, "PROVINCE_NME", "PROVINCE_ID", "", "", false);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BoundFromCity(ds.Tables[0].Rows[0]["PROVINCE_ID"].ToString());
                }
                else
                {
                    BoundFromCity("NO_RECORD");
                }
            }
        }

        private void BoundFromCity(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplFROM_CITY_ID", ds, "CITY_NME", "CITY_ID", "", "", false); 
            }
        }

        private void BoundLevelOne()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT LEVEL_ONE_ID,LEVEL_ONE_NME FROM SHP_GOODS_TYPE_LEVEL_ONE ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplLEVEL_ONE_ID", ds, "LEVEL_ONE_NME", "LEVEL_ONE_ID", "", "", false);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BoundLevelTwo(ds.Tables[0].Rows[0]["LEVEL_ONE_ID"].ToString());
                }
                else
                {
                    BoundLevelTwo("NO_RECORD");
                }
            }
        }
        private void BoundLevelTwo(string strOneId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT LEVEL_TWO_ID,LEVEL_TWO_NME FROM SHP_GOODS_TYPE_LEVEL_TWO WHERE (LEVEL_ONE_ID='" + strOneId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplLEVEL_TWO_ID", ds, "LEVEL_TWO_NME", "LEVEL_TWO_ID", "", "", false);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BoundLevelThree(ds.Tables[0].Rows[0]["LEVEL_TWO_ID"].ToString());
                }
                else
                {
                    BoundLevelThree("NO_RECORD");
                }
            }
        }
        private void BoundLevelThree(string strTwoId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT LEVEL_THREE_ID,LEVEL_THREE_NME FROM SHP_GOODS_TYPE_LEVEL_THREE WHERE (LEVEL_TWO_ID='" + strTwoId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplLEVEL_THREE_ID", ds, "LEVEL_THREE_NME", "LEVEL_THREE_ID", "", "", false);
            }
        }

        private void BoundCtLevelOne()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT CT_LEVEL_ONE_ID,CT_LEVEL_ONE_NME FROM SHP_COMMERCIAL_GOODS_LEVEL_ONE WHERE COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + "'";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCT_LEVEL_ONE_ID", ds, "CT_LEVEL_ONE_NME", "CT_LEVEL_ONE_ID", "", "", false);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BoundCtLevelTwo(ds.Tables[0].Rows[0]["CT_LEVEL_ONE_ID"].ToString());
                }
                else
                {
                    BoundCtLevelTwo("NO_RECORD");
                }
            }
        }
        private void BoundCtLevelTwo(string strOneId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT CT_LEVEL_TWO_ID,CT_LEVEL_TWO_NME FROM SHP_COMMERCIAL_GOODS_LEVEL_TWO WHERE (CT_LEVEL_ONE_ID='" + strOneId + "') AND COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + "'";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCT_LEVEL_TWO_ID", ds, "CT_LEVEL_TWO_NME", "CT_LEVEL_TWO_ID", "", "", false);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BoundCtLevelThree(ds.Tables[0].Rows[0]["CT_LEVEL_TWO_ID"].ToString());
                }
                else
                {
                    BoundCtLevelThree("NO_RECORD");
                }
            }
        }
        private void BoundCtLevelThree(string strTwoId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT CT_LEVEL_THREE_ID,CT_LEVEL_THREE_NME FROM SHP_COMMERCIAL_GOODS_LEVEL_THREE WHERE (CT_LEVEL_TWO_ID='" + strTwoId + "') AND COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + "'";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCT_LEVEL_THREE_ID", ds, "CT_LEVEL_THREE_NME", "CT_LEVEL_THREE_ID", "", "", false);
            }
        }

        /// <summary>
        /// 商品属性
        /// </summary>
        private void doLevelSet(string strId, string strJY_LEVEL1, string strJY_LEVEL2, string strJY_LEVEL3, string strCT_LEVEL1, string strCT_LEVEL2, string strCT_LEVEL3)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oCBizShpGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                //string strJY_LEVEL1, strJY_LEVEL2, strJY_LEVEL3, strCT_LEVEL1, strCT_LEVEL2, strCT_LEVEL3;
                //strJY_LEVEL1 = this.GetParamHtValue("JY_LEVEL1");
                //strJY_LEVEL2 = this.GetParamHtValue("JY_LEVEL2");
                //strJY_LEVEL3 = this.GetParamHtValue("JY_LEVEL3");
                //strCT_LEVEL1 = this.GetParamHtValue("CT_LEVEL1");
                //strCT_LEVEL2 = this.GetParamHtValue("CT_LEVEL2");
                //strCT_LEVEL3 = this.GetParamHtValue("CT_LEVEL3");

                DataSet ds = oCBizShpGoodsInfo.SelectGoodsAttrForPublic(strId
                    , strJY_LEVEL1
                    , strJY_LEVEL2
                    , strJY_LEVEL3
                    , strCT_LEVEL1
                    , strCT_LEVEL2
                    , strCT_LEVEL3);

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                this.ExecJScript(@" var data = " + strjsontem1 + @";
                                    var tpl =HtmlUtil.htmlDecode(document.getElementById('goods_attr_temp').innerHTML);  
                                    laytpl(tpl).render(data, function(render){
                                        document.getElementById('detail_list').innerHTML = render;
                                    });
                ");


                //string strCurrentJY_LEVEL = "<samp>&gt;</samp><span>" + strJY_LEVEL1 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL2 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL3 + "</span></span>";
                //string strCurrentCT_LEVEL = "<samp>&gt;</samp><span>" + strCT_LEVEL1 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL2 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL3 + "</span></span>";

                //this.ExecJScript("$('#sys_cate_name').html('" + strCurrentJY_LEVEL + "');");
                //this.ExecJScript("$('#ct_cate_name').html('" + strCurrentCT_LEVEL + "');");
            }
        }

        private void doNormsNmeSet(string strJY_LEVEL3)
        {
            using (Epm.AppLogic.Framework.CDbAccess odatatm=new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Data.DataSet ds = odatatm.QueryBySQL("SELECT * FROM SHP_GOODS_TYPE_LEVEL_THREE WHERE LEVEL_THREE_ID='" + strJY_LEVEL3 + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["NORMS_NME1"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#tdNORMS_NME1').html('" + ds.Tables[0].Rows[0]["NORMS_NME1"].ToString() + "');");
                    }
                    if (ds.Tables[0].Rows[0]["NORMS_NME2"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#tdNORMS_NME2').html('" + ds.Tables[0].Rows[0]["NORMS_NME2"].ToString() + "');");
                    }
                }
            }
        }

        /// <summary>
        /// 热点
        /// </summary>
        /// <param name="strId"></param>
        /// <param name="strJY_LEVEL1"></param>
        /// <param name="strJY_LEVEL2"></param>
        /// <param name="strJY_LEVEL3"></param>
        /// <param name="strCT_LEVEL1"></param>
        /// <param name="strCT_LEVEL2"></param>
        /// <param name="strCT_LEVEL3"></param>
        private void doHotLabelSet(string strId, string strJY_LEVEL1, string strJY_LEVEL2, string strJY_LEVEL3, string strCT_LEVEL1, string strCT_LEVEL2, string strCT_LEVEL3)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oCBizShpGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                //string strJY_LEVEL1, strJY_LEVEL2, strJY_LEVEL3, strCT_LEVEL1, strCT_LEVEL2, strCT_LEVEL3;
                //strJY_LEVEL1 = this.GetParamHtValue("JY_LEVEL1");
                //strJY_LEVEL2 = this.GetParamHtValue("JY_LEVEL2");
                //strJY_LEVEL3 = this.GetParamHtValue("JY_LEVEL3");
                //strCT_LEVEL1 = this.GetParamHtValue("CT_LEVEL1");
                //strCT_LEVEL2 = this.GetParamHtValue("CT_LEVEL2");
                //strCT_LEVEL3 = this.GetParamHtValue("CT_LEVEL3");

                DataSet ds = oCBizShpGoodsInfo.SelectGoodsHotLabelForPublic(strId
                    , strJY_LEVEL1
                    , strJY_LEVEL2
                    , strJY_LEVEL3
                    , strCT_LEVEL1
                    , strCT_LEVEL2
                    , strCT_LEVEL3);

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                this.ExecJScript(@" var data = " + strjsontem1 + @";
                                    var tpl =HtmlUtil.htmlDecode(document.getElementById('goods_hotlabel_temp').innerHTML);  
                                    laytpl(tpl).render(data, function(render){
                                         $('#listhotlabelshow').empty().html(render);
                                    });
                ");


                //string strCurrentJY_LEVEL = "<samp>&gt;</samp><span>" + strJY_LEVEL1 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL2 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL3 + "</span></span>";
                //string strCurrentCT_LEVEL = "<samp>&gt;</samp><span>" + strCT_LEVEL1 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL2 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL3 + "</span></span>";

                //this.ExecJScript("$('#sys_cate_name').html('" + strCurrentJY_LEVEL + "');");
                //this.ExecJScript("$('#ct_cate_name').html('" + strCurrentCT_LEVEL + "');");
            }
        }
        
        /// <summary>
        /// 商品规格
        /// </summary>
        private void doShowGoodSpecSet(string strId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT PUB_NORMS_ID,PUB_NORMS_NME1,PUB_NORMS_NME2,GOODS_OLD_PRICE,GOODS_STRIKE_PRICE,GOODS_QTY,PRICE_DESC FROM SHP_GOODS_NORMS_PUBLIC WHERE (GOODS_ID='" + strId + "') ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"var data = " + strjsontem1 + @";
                                    var tpl =HtmlUtil.htmlDecode(document.getElementById('good_spec_temp').innerHTML);  
                                    laytpl(tpl).render(data, function(render){
                                        $('#good_spec_tbody').empty().html(render);
                                    });
                ");
            }
        }

//        private void doShowGoodBrand()
//        {
//            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oCBizShpGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
//            {
//                DataSet ds = oCBizShpGoodsInfo.SelectGoodBrandForPublish(this.GetParamHtValue("ID"));
//                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
//                this.ExecJScript(@" var data = " + strjsontem1 + @";
//                                    var tpl =HtmlUtil.htmlDecode(document.getElementById('good_brand_temp').innerHTML);  
//                                    laytpl(tpl).render(data, function(render){
//                                        document.getElementById('good_brand').innerHTML = render;
//                                    });
//                ");
//            } 
//        }

        //private void doShowGoodPublish()
        //{
        //    string strJY_LEVEL1, strJY_LEVEL2, strJY_LEVEL3, strCT_LEVEL1, strCT_LEVEL2, strCT_LEVEL3;
        //    strJY_LEVEL1 = this.GetParamHtValue("JY_LEVEL1");
        //    strJY_LEVEL2 = this.GetParamHtValue("JY_LEVEL2");
        //    strJY_LEVEL3 = this.GetParamHtValue("JY_LEVEL3");
        //    strCT_LEVEL1 = this.GetParamHtValue("CT_LEVEL1");
        //    strCT_LEVEL2 = this.GetParamHtValue("CT_LEVEL2");
        //    strCT_LEVEL3 = this.GetParamHtValue("CT_LEVEL3");

        //    string strCurrentJY_LEVEL = "<samp>&gt;</samp><span>" + strJY_LEVEL1 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL2 + "</span><samp>&gt;</samp><span>" + strJY_LEVEL3 + "</span></span>";
        //    string strCurrentCT_LEVEL = "<samp>&gt;</samp><span>" + strCT_LEVEL1 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL2 + "</span><samp>&gt;</samp><span>" + strCT_LEVEL3 + "</span></span>";

        //    this.ExecJScript("$('#sys_cate_name').html('" + strCurrentJY_LEVEL + "');");
        //    this.ExecJScript("$('#ct_cate_name').html('" + strCurrentCT_LEVEL + "');");
        //}


        private void ShowData(string strId)
        {
            //using (Epm.AppLogic.Framework.CDbAccess oacc = new Epm.AppLogic.Framework.CDbAccess())
            //{
            //    DataSet dstm333 = oacc.QueryBySQL("SELECT SCORE_QTY FROM SHP_COMMERCIAL_TENANT_INFO WHERE COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + "'");
            //    if (dstm333.Tables[0].Rows.Count>0)
            //    {
            //        this.SetContrlValue("spnSCORE_QTY", dstm333.Tables[0].Rows[0]["SCORE_QTY"].ToString());
            //    }
            //}

            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCBizShpCommercialTenantInfo = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                int iAllowQty = oCBizShpCommercialTenantInfo.GetShopScoreAllowUseQty(this.oUserInfo.OperDealerId.Split(',')[0]);
                this.SetContrlValue("spnSCORE_QTY", iAllowQty.ToString());
            }
            if (strId == string.Empty)
            {
                this.ExecJScript(" $('#selectLevel').show();");
                doShowGoodSpecSet(strId);
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                DataSet dsGoods = oGoodsInfo.SelectById(strId);
                DataRow drGoods;

                if (dsGoods.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript("window.location.href = '/d_CommGoodsPublic.htm';");
                    return;
                }
                else
                {
                    drGoods = dsGoods.Tables[0].Rows[0];
                }
                this.ExecJScript("setShowGoodsDiv();");

                this.SetContrlValue("txtGOODS_NME", drGoods["GOODS_NME"].ToString());
                this.SetContrlValue("txtSTYLE_NME", drGoods["STYLE_NME"].ToString());
                this.SetContrlValue("txtGOODS_DESC", drGoods["GOODS_DESC"].ToString());
                this.SetContrlValue("dplBRAND_ID", drGoods["BRAND_ID"].ToString());
                this.SetContrlValue("dplSHIP_ADDRESS_ID", drGoods["SHIP_ADDRESS_ID"].ToString());
                this.SetContrlValue("txtSCORE_NUM", drGoods["SCORE_NUM"].ToString());
                this.SetContrlValue("dplLOGISTICS_TEMPLATE_ID", drGoods["LOGISTICS_TEMPLATE_ID"].ToString());
                //this.SetContrlValue("txtFREIGHT", drGoods["FREIGHT"].ToString());

                doLevelSet(strId, drGoods["LEVEL_ONE_ID"].ToString(), drGoods["LEVEL_TWO_ID"].ToString(), drGoods["LEVEL_THREE_ID"].ToString(), drGoods["CT_LEVEL_ONE_ID"].ToString(), drGoods["CT_LEVEL_TWO_ID"].ToString(), drGoods["CT_LEVEL_THREE_ID"].ToString());
                doHotLabelSet(strId, drGoods["LEVEL_ONE_ID"].ToString(), drGoods["LEVEL_TWO_ID"].ToString(), drGoods["LEVEL_THREE_ID"].ToString(), drGoods["CT_LEVEL_ONE_ID"].ToString(), drGoods["CT_LEVEL_TWO_ID"].ToString(), drGoods["CT_LEVEL_THREE_ID"].ToString());
                doNormsNmeSet(drGoods["LEVEL_THREE_ID"].ToString());
                
                doShowGoodSpecSet(strId);
                LoadGoodsImgShow(strId);

                BoundLevelOne();
                BoundCtLevelOne();
                BoundLevelTwo(drGoods["LEVEL_ONE_ID"].ToString());
                BoundCtLevelTwo(drGoods["CT_LEVEL_ONE_ID"].ToString());
                BoundLevelThree(drGoods["LEVEL_TWO_ID"].ToString());
                BoundCtLevelThree(drGoods["CT_LEVEL_TWO_ID"].ToString());



                this.SetContrlValue("dplLEVEL_ONE_ID", drGoods["LEVEL_ONE_ID"].ToString());
                this.SetContrlValue("dplLEVEL_TWO_ID", drGoods["LEVEL_TWO_ID"].ToString());
                this.SetContrlValue("dplLEVEL_THREE_ID", drGoods["LEVEL_THREE_ID"].ToString());
                this.SetContrlValue("dplCT_LEVEL_ONE_ID", drGoods["CT_LEVEL_ONE_ID"].ToString());
                this.SetContrlValue("dplCT_LEVEL_TWO_ID", drGoods["CT_LEVEL_TWO_ID"].ToString());
                this.SetContrlValue("dplCT_LEVEL_THREE_ID", drGoods["CT_LEVEL_THREE_ID"].ToString());

                this.ExecJScript("setLevelDescShow();");

                string strGoodsDesc = "";
                string strVpathtem1 = drGoods["GOODS_DESC_URL"].ToString();
                string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpathtem1);
                if (System.IO.File.Exists(strFFullPath))
                {
                    strGoodsDesc = System.IO.File.ReadAllText(strFFullPath, System.Text.Encoding.UTF8);//System.Text.Encoding.GetEncoding(936)
                }
                this.ExecJScript("SetEditHtml(\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(strGoodsDesc) + "\");");

            }

        }

        private void LoadGoodsImgShow(string strId)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsImg oDataObj = new AppLogic.DataAlc.CBizShpGoodsImg())
            {
                DataSet   dsItem = oDataObj.SelectByGoodsId(strId);
                for (int i = 0; i < dsItem.Tables[0].Rows.Count; i++)
                {
                    this.ExecJScript("selImg('" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dsItem.Tables[0].Rows[i]["IMG_URL"].ToString()) + "');");
                }
            }
        }

        //private void LoadLevelShow(string strId)
        //{
        //    using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
        //    {
        //        DataSet dsGoods = oGoodsInfo.SelectById(strId);
        //        DataRow drGoods;

        //        if (dsGoods.Tables[0].Rows.Count <= 0)
        //        { 
        //            return;
        //        }
        //        else
        //        {
        //            drGoods = dsGoods.Tables[0].Rows[0];
        //        }
        //        BoundLevelOne();
        //        BoundCtLevelOne();
        //        BoundLevelTwo(drGoods["LEVEL_ONE_ID"].ToString());
        //        BoundCtLevelTwo(drGoods["CT_LEVEL_ONE_ID"].ToString());
        //        BoundLevelThree(drGoods["LEVEL_TWO_ID"].ToString());
        //        BoundCtLevelThree(drGoods["CT_LEVEL_TWO_ID"].ToString());

                 

        //        this.SetContrlValue("dplLEVEL_ONE_ID", drGoods["LEVEL_ONE_ID"].ToString());
        //        this.SetContrlValue("dplLEVEL_TWO_ID", drGoods["LEVEL_TWO_ID"].ToString());
        //        this.SetContrlValue("dplLEVEL_THREE_ID", drGoods["LEVEL_THREE_ID"].ToString());
        //        this.SetContrlValue("dplCT_LEVEL_ONE_ID", drGoods["CT_LEVEL_ONE_ID"].ToString());
        //        this.SetContrlValue("dplCT_LEVEL_TWO_ID", drGoods["CT_LEVEL_TWO_ID"].ToString());
        //        this.SetContrlValue("dplCT_LEVEL_THREE_ID", drGoods["CT_LEVEL_THREE_ID"].ToString());
        //    }
        //}

        private void doSave()
        {

            string strId = this.GetParamHtValue("ID");
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo oGoodsInfo = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                DataSet dsGoods ;
                DataRow drGoods;
                if (strId == "")
                {
                    dsGoods = new msBsc.Xsd.DataTds.CDmShpGoodsInfo();
                }
                else
                {
                    dsGoods = oGoodsInfo.SelectById(strId);
                }

                bool blNew = false;
                if (dsGoods.Tables[0].Rows.Count <= 0)
                {
                    drGoods = dsGoods.Tables[0].NewRow();
                    drGoods["GOODS_ID"] = oGoodsInfo.DataObj.NewID();
                    drGoods["COMM_TENANT_ID"] = this.oUserInfo.OperDealerId.Split(',')[0];
                    drGoods["MALL_NO"] = "0";
                    dsGoods.Tables[0].Rows.Add(drGoods);
                    blNew = true;
                }
                else
                {
                    drGoods = dsGoods.Tables[0].Rows[0];
                    if (drGoods["COMM_TENANT_ID"].ToString() != this.oUserInfo.OperDealerId.Split(',')[0])
                    {
                        Alert("单前商品不是本店铺的！");
                        return;
                    }
                }
                msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(this.htPostDataRow, drGoods);

                string strGoodsDesc = this.GetPostHtValue("GOODS_HTML");

                string strVpath = drGoods["GOODS_DESC_URL"].ToString();
                if (strVpath == string.Empty)
                {
                    strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + GetRadomString() + ".html";
                    drGoods["GOODS_DESC_URL"] = strVpath;
                }
                string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
                System.IO.File.WriteAllText(strFFullPath, strGoodsDesc, System.Text.Encoding.UTF8);
                 
                
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                { 


                    strId = drGoods["GOODS_ID"].ToString();

                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsAttribute oGoodsAttr = new AppLogic.DataAlc.CBizShpGoodsAttribute())
                    {
                        DataSet dsGoodsAttr;
                        if (blNew)
                        {
                            dsGoodsAttr = new msBsc.Xsd.DataTds.CDmShpGoodsAttribute();
                        }
                        else
                        {
                            dsGoodsAttr = oGoodsAttr.SelectByGoodsId(strId);
                        }

                        System.Collections.Generic.List<string> lsAttrId = new System.Collections.Generic.List<string>();
                        int iAttrCount = int.Parse(GetPostHtValue("GOODS_ATTR_COUNT"));
                        for (int i = 0; i < iAttrCount; i++)
                        {
                            string strAttrId=GetPostHtValue("GOODS_ATTRIBUTE_ID" + i.ToString());
                            DataRow[] arrrowtem1 = dsGoodsAttr.Tables[0].Select("GOODS_ATTRIBUTE_ID='" + strAttrId + "'");
                            DataRow drAttr;
                            if (arrrowtem1.Length > 0)
                            {
                                drAttr = arrrowtem1[0]; 
                            }
                            else 
                            {
                                drAttr = dsGoodsAttr.Tables[0].NewRow();
                                drAttr["GOODS_ATTRIBUTE_ID"] = oGoodsAttr.DataObj.NewID();
                                drAttr["GOODS_ID"] = strId;
                                if (strAttrId == string.Empty)
                                {
                                    drAttr["ATTR_TEMPLATE_ID"] = "";
                                    drAttr["IS_CUSTOM"] = true;
                                }
                                else
                                {
                                    drAttr["ATTR_TEMPLATE_ID"] = strAttrId;
                                    drAttr["IS_CUSTOM"] = false;
                                }
                                dsGoodsAttr.Tables[0].Rows.Add(drAttr);
                            }
                            drAttr["SEQ_NUM"] = i;
                            drAttr["ATTRIBUTE_NME"] = GetPostHtValue("ATTRIBUTE_NME" + i.ToString());
                            drAttr["ATTRIBUTE_VALUE"] = GetPostHtValue("ATTRIBUTE_VALUE" + i.ToString());

                            lsAttrId.Add(drAttr["GOODS_ATTRIBUTE_ID"].ToString());
                        }

                        DataRow[] arrdelattr= dsGoodsAttr.Tables[0].Select("GOODS_ATTRIBUTE_ID NOT IN ('" + string.Join("','",lsAttrId.ToArray()) + "')");
                        for (int i = arrdelattr.Length - 1; i >= 0; i--)
                        {
                            arrdelattr[i].Delete();
                        }
                        oGoodsAttr.Update(dsGoodsAttr);

                    }


                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsHotLabel oGoodsAttr = new AppLogic.DataAlc.CBizShpGoodsHotLabel())
                    {
                        DataSet dsGoodsAttr;
                        if (blNew)
                        {
                            dsGoodsAttr = new msBsc.Xsd.DataTds.CDmShpGoodsHotLabel();
                        }
                        else
                        {
                            dsGoodsAttr = oGoodsAttr.SelectByGoodsId(strId);
                        }

                        System.Collections.Generic.List<string> lsAttrId = new System.Collections.Generic.List<string>();
                        int iAttrCount = int.Parse(GetPostHtValue("GOODS_HOT_LABEL_COUNT"));
                        for (int i = 0; i < iAttrCount; i++)
                        {
                            string strAttrId = GetPostHtValue("LABEL_TEMP_ID" + i.ToString());
                            lsAttrId.Add(strAttrId);
                            DataRow[] arrrowtem1 = dsGoodsAttr.Tables[0].Select("LABEL_TEMP_ID='" + strAttrId + "'");
                            DataRow drAttr;
                            if (arrrowtem1.Length > 0)
                            {
                                continue;
                            }
                            else
                            {
                                drAttr = dsGoodsAttr.Tables[0].NewRow();
                                drAttr["LABEL_ID"] = oGoodsAttr.DataObj.NewID();
                                drAttr["GOODS_ID"] = strId;
                                drAttr["LABEL_TEMP_ID"] = strAttrId;
                                drAttr["LABEL_NME"] = GetPostHtValue("LABEL_NME" + i.ToString()); 
                            drAttr["SEQ_NUM"] = i; 
                                dsGoodsAttr.Tables[0].Rows.Add(drAttr);
                            }

                        }

                        DataRow[] arrdelattr = dsGoodsAttr.Tables[0].Select("LABEL_TEMP_ID NOT IN ('" + string.Join("','", lsAttrId.ToArray()) + "')");
                        for (int i = arrdelattr.Length - 1; i >= 0; i--)
                        {
                            arrdelattr[i].Delete();
                        }
                        oGoodsAttr.Update(dsGoodsAttr);

                    }



                    int iTotalQty = 0;
                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsNormsPublic oGoodsNorms = new AppLogic.DataAlc.CBizShpGoodsNormsPublic())
                    {
                        DataSet dsGoodsNorms;
                        if (blNew)
                        {
                            dsGoodsNorms = new msBsc.Xsd.DataTds.CDmShpGoodsNormsPublic();
                        }
                        else
                        {
                            dsGoodsNorms = oGoodsNorms.SelectByGoodsId(strId);
                        }

                        System.Collections.Generic.List<string> lsNormsId = new System.Collections.Generic.List<string>();
                        int iNormsCount = int.Parse(GetPostHtValue("GOODS_NORMS_COUNT"));
                        if (iNormsCount < 1)
                        {
                            throw new Exception("至少填写1条商品规格记录！");
                        }

                        System.Collections.Generic.List<string> lsNormsVali = new System.Collections.Generic.List<string>();
                        for (int i = 0; i < iNormsCount; i++)
                        {
                            string strNormsId = GetPostHtValue("PUB_NORMS_ID_" + i.ToString());
                            DataRow[] arrrowtem1 = dsGoodsNorms.Tables[0].Select("PUB_NORMS_ID='" + strNormsId + "'");
                            DataRow drNorms;
                            if (arrrowtem1.Length > 0)
                            {
                                drNorms = arrrowtem1[0];
                            }
                            else
                            {
                                drNorms = dsGoodsNorms.Tables[0].NewRow();
                                drNorms["PUB_NORMS_ID"] = oGoodsNorms.DataObj.NewID();
                                drNorms["GOODS_ID"] = strId;
                                drNorms["PUB_NORMS_NME1"] = strId;
                                dsGoodsNorms.Tables[0].Rows.Add(drNorms);
                            }
                            drNorms["PUB_NORMS_NME1"] = GetPostHtValue("PUB_NORMS_NME1_" + i.ToString());
                            drNorms["PUB_NORMS_NME2"] = GetPostHtValue("PUB_NORMS_NME2_" + i.ToString());
                            drNorms["PRICE_DESC"] = GetPostHtValue("PRICE_DESC_" + i.ToString());
                            drNorms["GOODS_OLD_PRICE"] = GetPostHtValue("GOODS_OLD_PRICE_" + i.ToString()) == string.Empty ? 0M : decimal.Parse(GetPostHtValue("GOODS_OLD_PRICE_" + i.ToString()));
                            drNorms["GOODS_STRIKE_PRICE"] = GetPostHtValue("GOODS_STRIKE_PRICE_" + i.ToString()) == string.Empty ? 0M : decimal.Parse(GetPostHtValue("GOODS_STRIKE_PRICE_" + i.ToString())); 
                            drNorms["GOODS_QTY"] = GetPostHtValue("GOODS_QTY_" + i.ToString())==string.Empty?0:int.Parse(GetPostHtValue("GOODS_QTY_" + i.ToString()));
                            iTotalQty += (int)drNorms["GOODS_QTY"];
                            drNorms["SEQ_NUM"] = i;

                            if (lsNormsVali.Contains(drNorms["PUB_NORMS_NME1"].ToString() + "-" + drNorms["PUB_NORMS_NME2"].ToString()))
                            {
                                throw new Exception("商品规格的组合不能有重复，请重新录入！");
                            }
                            else
                            {
                                lsNormsVali.Add(drNorms["PUB_NORMS_NME1"].ToString() + "-" + drNorms["PUB_NORMS_NME2"].ToString());
                            }

                            lsNormsId.Add(drNorms["PUB_NORMS_ID"].ToString());
                            if (i == 0)
                            {
                                drGoods["GOODS_OLD_PRICE"] = drNorms["GOODS_OLD_PRICE"];
                                drGoods["GOODS_STRIKE_PRICE"] = drNorms["GOODS_STRIKE_PRICE"];
                            }
                        }

                        DataRow[] arrdelnorms = dsGoodsNorms.Tables[0].Select("PUB_NORMS_ID NOT IN ('" + string.Join("','", lsNormsId.ToArray()) + "')");
                        for (int i = arrdelnorms.Length - 1; i >= 0; i--)
                        {
                            arrdelnorms[i].Delete();
                        }
                        drGoods["NORMS_COUNT"] = iNormsCount;
                        oGoodsNorms.Update(dsGoodsNorms);

                    }





                    using (msBsc.AppLogic.DataAlc.CBizShpGoodsImg oDataObj = new AppLogic.DataAlc.CBizShpGoodsImg())
                    {
                        DataSet dsItem;
                        if (blNew)
                        {
                            dsItem = new msBsc.Xsd.DataTds.CDmShpGoodsImg();
                        }
                        else
                        {
                            dsItem = oDataObj.SelectByGoodsId(strId);
                        }

                        System.Collections.Generic.List<string> lsIds = new System.Collections.Generic.List<string>();

                        int iItemCount = int.Parse(GetPostHtValue("GOODS_IMG_COUNT"));
                        for (int i = 0; i < iItemCount; i++)
                        {
                            DataRow drNewItem;
                            if (dsItem.Tables[0].Rows.Count > i)
                            {
                                drNewItem = dsItem.Tables[0].Rows[i];
                            }
                            else
                            {
                                drNewItem = dsItem.Tables[0].NewRow();
                                drNewItem["GOODS_IMG_ID"] = oDataObj.DataObj.NewID();
                                drNewItem["GOODS_ID"] = strId;
                                dsItem.Tables[0].Rows.Add(drNewItem);
                            }
                            drNewItem["SEQ_NUM"] = i;
                            drNewItem["IMG_URL"] = GetPostHtValue("GOODS_IMG_URL" + i.ToString());

                            if (i == 0)
                            {
                                drGoods["IMG_URL"] = drNewItem["IMG_URL"];
                            }
                             
                        }

                        for (int i = dsItem.Tables[0].Rows.Count - 1; i >= iItemCount; i--)
                        {
                            dsItem.Tables[0].Rows[i].Delete();
                        }

                        oDataObj.Update(dsItem);

                    }

                    //if(int.Parse(drGoods["SCORE_NUM"].ToString()==string.Empty?"0":drGoods["SCORE_NUM"].ToString())>0)
                    //{
                    //    DataSet dstm333 = oGoodsInfo.DataObj.QueryBySQL("SELECT SCORE_QTY FROM SHP_COMMERCIAL_TENANT_INFO WHERE COMM_TENANT_ID='" + drGoods["COMM_TENANT_ID"].ToString() + "'");
                    //    if ((int)dstm333.Tables[0].Rows[0]["SCORE_QTY"] < iTotalQty * (int)drGoods["SCORE_NUM"])
                    //    {
                    //        throw new Exception("您当前店铺的可用积分数量太少，不满足你的积分赠送需求，请重新录入！");
                    //    }
                    //}
                    oGoodsInfo.Update(dsGoods);

                    using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCBizShpCommercialTenantInfo = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
                    {
                        int iAllowQty = oCBizShpCommercialTenantInfo.GetShopScoreAllowUseQty(this.oUserInfo.OperDealerId.Split(',')[0]);
                        if (iAllowQty < 0)
                        {
                            throw new Exception("您当前店铺的可用积分数量太少，不满足你的积分赠送需求，请重新录入！");
                        }
                        else
                        {
                            this.SetContrlValue("spnSCORE_QTY", iAllowQty.ToString());
                        }
                    }
                    ts.Complete(); 
                }

            }



            this.ExecJScript("layer.msg('成功保存商品！',1,{type:1,shade:false});");

             
        }

        public override void DoAction()
        {
            if (this.strAction == "dplLEVEL_ONE_ID_change")
            {
                BoundLevelTwo(this.GetParamHtValue("ONE_ID"));
            }
            else if (this.strAction == "dplLEVEL_TWO_ID_change")
            {
                BoundLevelThree(this.GetParamHtValue("TWO_ID"));
            }
            else if (this.strAction == "dplCT_LEVEL_ONE_ID_change")
            {
                BoundCtLevelTwo(this.GetParamHtValue("ONE_ID"));
            }
            else if (this.strAction == "dplCT_LEVEL_TWO_ID_change")
            {
                BoundCtLevelThree(this.GetParamHtValue("TWO_ID"));
            }
            else if (this.strAction == "dplFROM_PROVINCE_ID_change")
            {
                BoundFromCity(this.GetParamHtValue("PROVINCE_ID"));
            }
            else if (this.strAction == "doSelectLevel")
            {
                //string strJY_LEVEL1, strJY_LEVEL2, strJY_LEVEL3, strCT_LEVEL1, strCT_LEVEL2, strCT_LEVEL3;
                //strJY_LEVEL1 = this.GetParamHtValue("JY_LEVEL1");
                //strJY_LEVEL2 = this.GetParamHtValue("JY_LEVEL2");
                //strJY_LEVEL3 = this.GetParamHtValue("JY_LEVEL3");
                //strCT_LEVEL1 = this.GetParamHtValue("CT_LEVEL1");
                //strCT_LEVEL2 = this.GetParamHtValue("CT_LEVEL2");
                //strCT_LEVEL3 = this.GetParamHtValue("CT_LEVEL3");
                doLevelSet(this.GetParamHtValue("ID"), this.GetParamHtValue("JY_LEVEL1"), this.GetParamHtValue("JY_LEVEL2"), this.GetParamHtValue("JY_LEVEL3"), this.GetParamHtValue("CT_LEVEL1"), this.GetParamHtValue("CT_LEVEL2"), this.GetParamHtValue("CT_LEVEL3"));

                doHotLabelSet(this.GetParamHtValue("ID"), this.GetParamHtValue("JY_LEVEL1"), this.GetParamHtValue("JY_LEVEL2"), this.GetParamHtValue("JY_LEVEL3"), this.GetParamHtValue("CT_LEVEL1"), this.GetParamHtValue("CT_LEVEL2"), this.GetParamHtValue("CT_LEVEL3"));
                doNormsNmeSet(this.GetParamHtValue("JY_LEVEL3"));

           }
            else if (this.strAction == "dogetBrandAndNorms")
            {
                doShowGoodSpecSet(this.GetParamHtValue("ID"));
                //doShowGoodBrand();
            }
            else if (this.strAction == "doShowAll")
            {
                ShowData(this.GetParamHtValue("ID"));
            }
            //else if (this.strAction == "doLoadLevelShow")
            //{
            //    LoadLevelShow(this.GetParamHtValue("ID"));
            //}
            //else if (this.strAction == "mPublishGoods")
            //{
            //    //doShowGoodPublish();
            //    doLevelSet();
            //    doShowGoodSpecSet();
            //    doShowGoodBrand();
            //}
            else if (this.strAction == "doSaveGoodsPub")
            {
                doSave();
            }
        }

    }
}